//初始化
mui.init();
var mask = mui.createMask(); //遮罩层
//获取本地储存数据
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var is_realname, pay_password, description;
var status = 0; //修改手机号审核状态
var pwds;
var numVal;
//返回user页面
$(document).on('tap', '.go_user', function() {
	mui.back();
})



//userinfo接口调用

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		txtM = self.partition_type;
		$('.account_span').text(txtM+'元/g')
		plus.nativeUI.closeWaiting(); //关闭等待
		mask.close(); //关闭遮罩层
		//页面刷新
		window.addEventListener('refresh_method', function(e) { //执行刷新
			location.reload();
		});	
	});
}
//判断 数量是否是1000 的倍数
function isTenMutiple(number){
	var isTen=number%1000;
	if(isTen==0){
		return true;
	}else{
		return false;
	}
}

//立即支付
var totalM;
$('#pay_pwd').change(function(){
    numVal = $('#pay_pwd').val();
	var numbool = isTenMutiple(numVal);
	if($('#pay_pwd').val() == ''){
		mui.alert('请先选择数量')
		return false
	}
	if(numbool==true){
		totalM = numVal*txtM
		$('#account_span').text(totalM)
	}else{
	  mui.alert('请先输入1000的整数倍')	
	}
})
document.getElementById("pay_sureBtn").addEventListener("tap", function() {
	if($('#pay_pwd').val() == ''){
		mui.alert('请先选择数量')
		return false
	}
	numVal = $('#pay_pwd').val();
	var numbool = isTenMutiple(numVal);
	if($('#pay_pwd').val() == ''){
		mui.alert('请先选择数量')
		return false
	}
	if(numbool==true){
		totalM = numVal*txtM
		$('#account_span').text(totalM)
	}else{
	  mui.alert('请先输入1000的整数倍')	
	}
	payPanel.showPanel(function(){
		
	})
//	mui.openWindow({
//		url: '../my/silver_result.html',
//		createNew: true
//	})
});



//输入支付密码  开始
 /**==================================================*/
(function() {
	window.payPanel = {
		/**
		 * 显示输入面板，指定输入完成后的回调事件和点击取消的回调时间
		 * @param {Object} _inputOverFn
		 * @param {Object} _cancelFn
		 */
		showPanel: function(_inputOverFn, _cancelFn) {
			//输入完成后的回调
			panelObj.inputOverFn = _inputOverFn;
			
			//取消后的回调
			panelObj.cancelFn = _cancelFn;

			document.getElementsByClassName("pays")[0].style.display = "block";
			this.isShow = true; 
		},
		/**
		 * 关闭输入面板
		 */
		closePanel: function() {
			document.getElementsByClassName("pays")[0].style.display = "block";
			panelObj.clearAll();
			this.isShow = false;
		},
		isShow:false
	}
	//输入面板的对象及其所有方法
	var panelObj = {
		//输入面板内容
		template: "<div class='payPanel'><div class='pass'><i class='mui-icon mui-icon-closeempty close'></i><h4>请输入支付密码</h4><p id='pay_price'></p><ul><li></li><li></li><li></li><li></li><li></li><li></li></ul></div><div class='inputPanel'><div><ul><li><a data-value='1'>1</a></li><li><a data-value='2'>2</a></li><li><a data-value='3'>3</a></li><li><a data-value='4'>4</a></li><li><a data-value='5'>5</a></li><li><a data-value='6'>6</a></li><li><a data-value='7'>7</a></li><li><a data-value='8'>8</a></li><li><a data-value='9'>9</a></li><li class='cancel'><a data-value='cancel'>取消</a></li><li><a data-value='0'>0</a></li><li><a data-value='delete'>删除</a></li></ul></div></div></div>",
		//初始化面板，将面板添加到页面
		init: function() {
			var div = document.createElement("div");
			div.className = "pays";
			div.innerHTML = this.template;
			document.body.appendChild(div);
			this.addEvent();
		},
		//输入的密码值
		inputVal: "",
		//添加键盘点击事件
		addEvent: function() {
			var inputLi = document.getElementsByClassName("inputPanel")[0].getElementsByTagName("a");
			for(var i = 0, len = inputLi.length; i < len; i++) {
				var ele = inputLi[i];
				ele.addEventListener("touchstart", function(e) {
					e.preventDefault();
					var value = this.getAttribute("data-value");
					if(value == "cancel") {
						//取消支付
						panelObj.clearAll();
						payPanel.closePanel();
						panelObj.cancelFn!=undefined?panelObj.cancelFn():"";

					} else if(value == "delete") {
						//删除
						var len = panelObj.inputVal.length;
						panelObj.inputVal = panelObj.inputVal.substr(0, len - 1);
						panelObj.clearInputContent(len - 1);

					} else {
						if(panelObj.inputVal.length >= 6) return;
						panelObj.inputVal += value;
						//设置输入框内容
						panelObj.setInputContent();
						if(panelObj.inputVal.length == 6) {
							//延迟2毫秒,保证用户可以看到输入了第六位
							setTimeout(function(){
								pwds = panelObj.inputVal;
								panelObj.inputOverFn!=undefined?panelObj.inputOverFn(panelObj.inputVal):"";
								panelObj.clearAll();
								console.log(pwds)
								var dataM = {
									pay_password : pwds,
									silver_price : txtM,
									silver_number : numVal,
									sum_price :totalM
								}
								mui.ajax(order_url + '/api/Silver/silverPay', { //4.确认支付（判断支付密码是否正确）接口
									data:dataM,
									dataType: 'json', //服务器返回json格式数据
									type: 'post', //HTTP请求类型
									timeout: 10000, //超时时间设置为10秒；
									success: function(data) {
										console.log(JSON.stringify(data))
										plus.nativeUI.closeWaiting(); //关闭等待
										mask.close(); //关闭遮罩层
									if(data.code == 10001){				
										payPanel.closePanel()
										mui.alert(data.msg);
										return false;
									}
									if(data.code == 10002){				
										payPanel.closePanel()
										mui.alert(data.msg);
										return false;
									}
									if(data.code == 10003){	
										payPanel.closePanel()
										mui.alert(data.msg);
										return false;
									}
								    if(data.code == 10000) {
								    	mui.alert('支付成功')
								    	mui.openWindow({ //跳转到我的订单
											url: "../my/silver_list.html",
											createNew: true
										});
								    }
									
									},
									error: function(xhr, type, errorThrown) { //异常处理；
										plus.nativeUI.closeWaiting(); //关闭等待
										mask.close(); //关闭遮罩层
										console.log('确认密码接口失败');
							//			var error_msg = JSON.parse(xhr.responseText)
							//			mui.alert(error_msg.msg)
									}
								});
//								console.log(JSON.stringify(dataM))
							},200)
							
						}

					}

				})
			}
		},
		//给6个输入款填充内容
		setInputContent: function() {
			var index = panelObj.inputVal.length - 1;
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			passLi[index].className = " i-circle";
		},
		//给指定的输入款取消内容
		clearInputContent: function(_index) {
			if(_index<0)return;
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			passLi[_index].className = "";
		},
		//清除所有输入款内容
		clearAll: function() {
			this.inputVal = "";
			var passLi = document.getElementsByClassName("pass")[0].getElementsByTagName("li");
			for(var i = 0, len = passLi.length; i < len; i++) {
				passLi[i].className = "";
			}
		}, 
	}
	panelObj.init();
	// 点击 (x), 关闭弹窗
	$(document).on("tap", ".close", function() {
		panelObj.clearAll();
		//payPanel.closePanel();//关闭弹窗
		panelObj.cancelFn!=undefined?panelObj.cancelFn():""; 
		document.getElementsByClassName("pays")[0].style.display = "none"; 
		this.isShow = false;
		
		
	});
})(window)

/*==================================================
输入支付密码  结束
 ==================================================*/